﻿using System;
using System.Data;
using System.Collections.Generic;



namespace IncubatorServices
{
    public class User
    {
        public int id;
        public string fname;
        public string lname;
        public int company_id;
        public string username;
        public string password;
        public string hash;

        public int role_id;

        public User()
        {
        }

        public int RegisterNewUser(string fname_, string lname_, int company_id_,
            string username_, string password_, int role_id_)
        {
            //Return id of new user

            sqlManager sql = new sqlManager();
            string query = "SELECT id FROM users WHERE username = '" + username_ + "'";
            DataSet ds = sql.GetDataSet(query);
            if (ds.Tables[0].Rows.Count > 0)
            { //user already exists
                id = -1;
                return -1;
            }
            if (password_.Length < 4)
            { //password too short
                id = -1;
                return -1;
            }

            //VALIDATION: check if username is unique, password not blank, etc
            int id_ = createNew();
            id = id_;
            fname = fname_;
            lname = lname_;
            company_id = company_id_;
            username = username_;
            password = password_;
            //hash = hash_;
            //TODO: Generate hash and password
            role_id = role_id_;
            storeData();
            return id;
        }
        public void LoadData(int id_)
        {
            sqlManager sql = new sqlManager();
            string query = "SELECT fname, lname, company_id, username, password, hash, role_id FROM users WHERE id = '" + id_ + "'";

            DataSet ds = sql.GetDataSet(query);
            id = id_;
            this.fname = sqlManager.GetData(ds.Tables[0].Rows[0]["fname"]);
            this.lname = sqlManager.GetData(ds.Tables[0].Rows[0]["lname"]);
            Int32.TryParse(sqlManager.GetData(ds.Tables[0].Rows[0]["company_id"]), out this.company_id);
            this.username = sqlManager.GetData(ds.Tables[0].Rows[0]["username"]);
            this.password = sqlManager.GetData(ds.Tables[0].Rows[0]["password"]);
            this.hash = sqlManager.GetData(ds.Tables[0].Rows[0]["hash"]);
            this.role_id = Int32.Parse(sqlManager.GetData(ds.Tables[0].Rows[0]["role_id"]));
        }

        public void LoadUserByName(string name)
        {
            sqlManager sql = new sqlManager();
            string query = "SELECT id FROM users WHERE username = '" + name + "'";
            int id = 0;
            Int32.TryParse(sql.GetScalar(query), out id);
            if (id == 0) throw new Exception("User " + name + " does not exist.");
            LoadData(id);
        }
        public void storeData()
        {
            sqlManager sql = new sqlManager();
            string query = "UPDATE users SET fname = '" + fname + "', " +
                "lname = '" + lname + "', " +
                "company_id = '" + company_id + "'," +
                "username = '" + username + "'," +
                "password  = '" + password + "'," +
                "hash = '" + hash + "'," +
                "role_id = '" + role_id + "'" +
                "WHERE id = '" + id + "'";
            sql.NonQuery(query);
        }

        private int createNew()
        {
            sqlManager sql = new sqlManager();
            string query = "INSERT INTO users (fname, lname, role_id) VALUES('','',0)";
            sql.NonQuery(query);
            return Int32.Parse(sql.GetScalar("SELECT MAX(id) as max FROM users"));

        }

        public void deleteUser()
        {

            sqlManager sql = new sqlManager();
            string query = "DELETE FROM users WHERE id = '" + id + "'";
            sql.NonQuery(query);
        }

        

        public static List<User> getAllUsers()
        {
            List<User> lst = new List<User>();
            string query = "SELECT id FROM users";
            sqlManager sql = new sqlManager();

            DataSet ds = sql.GetDataSet(query);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                User u = new User();
                int id = sqlManager.GetDataInt(dr["id"]);
                u.LoadData(id);
                lst.Add(u);
            }
            return lst;
        }
    }
}